/****************************************************************************
 *
 *   Copyright (c) 2021 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#pragma once

#include <px4_platform_common/module_params.h>
#include "ActuatorEffectiveness.hpp"
#include <uORB/Subscription.hpp>
#include <uORB/topics/manual_control_setpoint.h>
#include <uORB/topics/vehicle_attitude.h>
#include <uORB/topics/vehicle_angular_velocity.h>
#include <uORB/topics/vehicle_local_position.h>

class ActuatorEffectivenessHetero10PWM : public ModuleParams, public ActuatorEffectiveness
{
public:
	ActuatorEffectivenessHetero10PWM(ModuleParams *parent)
		: ModuleParams(parent)
	{
		// 使用自定义PID实现，避免PID库编译问题
	}
	virtual ~ActuatorEffectivenessHetero10PWM() = default;

	bool getEffectivenessMatrix(Configuration &configuration, EffectivenessUpdateReason external_update) override;

	void updateSetpoint(const matrix::Vector<float, NUM_AXES> &control_sp, int matrix_index, ActuatorVector &actuator_sp,
			    const ActuatorVector &actuator_min, const ActuatorVector &actuator_max) override;

	const char *name() const override { return "Hetero10PWM"; }

private:
	// UORB订阅器 - 用于姿态稳定控制
	uORB::Subscription _uorb_attitude_sub{ORB_ID(vehicle_attitude)};
	uORB::Subscription _uorb_angular_velocity_sub{ORB_ID(vehicle_angular_velocity)};
	uORB::Subscription _uorb_local_position_sub{ORB_ID(vehicle_local_position)};

	// 姿态稳定相关变量
	float _last_yaw{0.0f};
	hrt_abstime _last_time{0};
	float _yaw_integral{0.0f};  // Yaw PID积分项

	// 位置控制相关变量
	float _target_x{0.0f}, _target_y{0.0f}, _target_z{0.0f};  // 目标位置
	float _x_integral{0.0f}, _y_integral{0.0f}, _z_integral{0.0f};  // 位置PID积分项
	bool _position_hold_enabled{false};  // 位置保持使能标志
	bool _altitude_hold_enabled{false};  // 高度保持使能标志
};
